package test;

public class VolatileExample {

    public static void main(String[] args) throws InterruptedException {
        VolatileExample example = new VolatileExample();
        Thread thread = new Thread(() -> {
            example.run();
        });
        thread.start();
        Thread.sleep(10000);
        example.stop();
    }
    private volatile boolean stop = false;

    public void stop() {
        stop = true;
    }
    public void run() {
        while (!stop) {
            // do something
        }
        System.out.println("Stopped");
    }

}
